package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dao.CustomerDao;
import com.example.demo.dao.ManagerDao;
import com.example.demo.model.Customer;
import com.example.demo.model.MD5Utils;
import com.example.demo.model.Manager;
import com.example.demo.service.CustomerService;
import com.example.demo.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.beans.Customizer;
import java.security.NoSuchAlgorithmException;
import java.util.List;


@Service
public class CustomerServiceimpl extends ServiceImpl<CustomerDao, Customer> implements CustomerService {
    @Resource
    private CustomerDao customerDao;

    public class UserNotFoundException extends Exception{
        String message;
        public UserNotFoundException(String x) {
            this.message=x;
            System.out.println(message);
        }

    }

    public class UpdateException extends Exception{
        String message;
        public UpdateException(String x) {
            this.message=x;
            System.out.println(message);
        }
    }

    public class PasswordNotMatchException extends Exception{
        String message;
        public PasswordNotMatchException(String x) {
            this.message=x;
            System.out.println(message);
        }
    }

    @Override
    public Customer doLogin(String name){
        Customer customer=customerDao.doLogin(name);
        return customer;
    }

    @Override
    public List<Customer> findAddress(Integer id) {
        return customerDao.findAddress(id);
    }

    @Override
    public Customer getCustomer(Integer uid) {
        return customerDao.getCustomer(uid);
    }

    @Override
    public void changePassword(Integer uid, String oldPassword, String newPassword) throws UserNotFoundException, PasswordNotMatchException, UpdateException, NoSuchAlgorithmException {
        Customer result = customerDao.fndByUid(uid);
        if(result == null) {
            throw new UserNotFoundException("用户数据不存在");
        }
        //原始密码和数据库中密码进行比较
        String oldMd5Password = MD5Utils.getMD5Str(oldPassword); //对输入的密码加密再和数据库中的密码比较
        if(!result.getPassword().equals(oldMd5Password)) {
            throw new PasswordNotMatchException("密码错误");
        }
        //将新的密码设置到数据库中，将新的密码进行加密再去更新
        String newMd5Password = MD5Utils.getMD5Str(newPassword) ;
        Integer rows = customerDao.updatePasswordByUid(uid, newMd5Password);

        if(rows != 1) {
            throw new UpdateException("更新数据产生未知的异常");
        }
    }

    @Override
    public Customer getByUid(Integer uid) throws UserNotFoundException {
       Customer result = customerDao.fndByUid(uid);
        if(result==null ) {
            throw new UserNotFoundException("用户数据不存在");
        }
        Customer customer = new Customer();
        customer.setName(result.getName());
        customer.setSex(result.getSex());
        customer.setSno(result.getSno());
        customer.setTelephone(result.getTelephone());
        customer.setAddress(result.getAddress());
        return customer;

    }

    @Override
    public void changeInfo(Integer uid, Customer customer) throws UserNotFoundException, UpdateException {
        Customer result = customerDao.fndByUid(uid);
        if(result==null ) {
            throw new UserNotFoundException("用户数据不存在");
        }
        result.setName(customer.getName());
        result.setSex(customer.getSex());
        result.setTelephone(customer.getTelephone());
        result.setSno(customer.getSno());
        result.setAddress(customer.getAddress());
        Integer rows = customerDao.updateInfoByUid(uid,result);
        if(rows != 1) {
            throw new UpdateException("更新数据产生的异常");
        }

    }


    @Override
    public Page<Customer> pageQueryCustomer(Page page, String name, String sno) {
        return customerDao.pageQueryCustomer(page,name,sno);
    }

    @Override
    @Cacheable(cacheNames = "customer")
    public Customer getOneCustomer(int id) {
        return this.getById(id);
    }

    @Override
    @Cacheable(cacheNames = "customerlist")
    public List<Customer> mylist() {
        return this.list();
    }



}
